var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const app = getApp()
// 触底上拉刷新 TODO 这里要将page传给服务器，作者没写
Page({
    data: {
        // 设备是否为苹果X
        isIphoneX: 0,
        // 地址列表
        addresses: [],
        nowAddress: 0,
        // 用户信息
        userInfo: {},
        // 判断当前界面是选取地址还是编辑收货地址
        type: 0,
        // 加载图标的位置
        sysHeight: 0,
        // 控制加载
        loading: 0,
    },
    // 跳转到编辑地址
    goAddressDetail: function (e) {
        let id = e.currentTarget.dataset.addressid;
        wx.navigateTo({
            url: '/pages/ucenter/address-detail/index?id=' + id,
        })
    },
    // 获取地址
    getAddresses() {
        let that = this;
        let userID = wx.getStorageSync('userID')
        util.requestApi(api.getUserAddress, {
            userID: userID
        }).then(function (res) {
            if (res.code == 200) {
                that.setData({
                    addresses: res.content,
                    loading: 1
                })
            }
        });
    },
    // 选择地址
    selectAddress: function (e) {
        let addressId = e.currentTarget.dataset.addressid
        wx.setStorageSync('addressId', addressId);
        wx.navigateBack();
    },
    onLoad: function (options) {
        // 获取传值
        let type = options.type;
        this.setData({
            type: type
        })
        // 获取设备信息
        let phone = wx.getStorageSync('isIphoneX')
        if (phone) {
            this.setData({
                isIphoneX: 1
            })
        }
    },
    onUnload: function () {},
    onShow: function () {
        // 获取设备信息
        let info = wx.getSystemInfoSync();
        let sysHeight = info.windowHeight - 100;
        this.setData({
            sysHeight: sysHeight,
        });
        // 获取地址
        this.getAddresses();
        // 获取地址id
        let addressId = wx.getStorageSync('addressId');
        if (addressId) {
            this.setData({
                nowAddress: wx.getStorageSync('addressId')
            });
        } else {
            this.setData({
                nowAddress: 0
            });
        }
    },
    // 添加地址
    addAddress: function () {
        wx.navigateTo({
            url: '/pages/ucenter/address-detail/index?id=' + 0,
        })
    },
    // 下拉刷新
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()
        this.getAddresses();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    }
})